﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using V5_Common;
using V5_Model;
using V5_BLL;

namespace V5_EShop.Admin
{
    public partial class Admin_Friend : BasicPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                Action = Input.SetString(Request["Action"]);
                switch (Action)
                {
                    case "edit":
                        this.MultiViewList.ActiveViewIndex = 1;
                        break;
                    default:
                        this.Bind_gvList();
                        break;
                }
            }
        }

        private void Bind_gvList()
        {
            this.MultiViewList.ActiveViewIndex = 0;

            int count = 0;
            this.gvList.DataSource = new BLLFriend().GetList(Pager.PageSize, Pager.CurrentPageIndex, "", ref count);
            this.Pager.RecordCount = count;
            this.gvList.DataBind();
        }

        private void Bind_DataEdit(int id)
        {
            this.MultiViewList.ActiveViewIndex = 1;
            ModelFriend model = new BLLFriend().GetModel(id);
            if (model != null)
            {
                this.txtID.Value = model.ID.ToString();
                this.txtFriendTitle.Text = model.FriendTitle;
                this.txtFriendUrl.Text = model.FriendUrl;
                this.txtFriendLogo.Text = model.FriendLogo;
                this.txtFriendReadme.Text = model.FriendReadme.ToString();
                this.ddlIsShow.SelectedValue = model.IsShow.ToString();

                this.btnSave.Text = "修改保存";
                this.lblTitle.Text = "添加友情链接";
            }
        }

        private void Bind_Delete(int id)
        {
            new BLLFriend().Delete(id);
            this.Bind_gvList();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int ID = Input.SetInt(this.txtID.Value);
            string FriendTitle = this.txtFriendTitle.Text;
            string FriendUrl = this.txtFriendUrl.Text;
            string FriendLogo = this.txtFriendLogo.Text;
            string FriendReadme = this.txtFriendReadme.Text;
            int IsShow = Input.SetInt(this.ddlIsShow.SelectedValue);
            BLLFriend bll = new BLLFriend();
            ModelFriend model = new ModelFriend();
            model.FriendTitle = FriendTitle;
            model.FriendUrl = FriendUrl;
            model.FriendLogo = FriendLogo;
            model.FriendReadme = FriendReadme;
            model.IsShow = IsShow;
            if (ID == 0)
            {
                model.ID = bll.GetMaxId();
                bll.Add(model);
            }
            else if (ID > 0)
            {
                model.ID = ID;
                bll.Update(model);
            }
            this.Bind_gvList();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string eName = Input.SetString(e.CommandName);
            int id = Input.SetInt(e.CommandArgument);
            switch (eName.Trim().ToLower())
            {
                case "go":
                    this.Bind_DataEdit(id);
                    break;
                case "del":
                    this.Bind_Delete(id);
                    break;
                default:
                    break;
            }
        }

        protected void Pager_PageChanged(object sender, EventArgs e)
        {
            this.Bind_gvList();
        }

    }
}
